﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace Kinect_Interface_Prototype_1
{
    public class ImageButton : PictureBox
    {
        private string imageFolder = Constants.FolderControlImagesFolder;

        public string BaseName { get; set; }

        private bool is_active;
        public bool isActive
        {
            get
            {
                return is_active;
            }
            set
            {
                is_active = value;
                if (value)
                {
                    SetToActivePicture();
                }
                else
                {
                    SetToInactivePicture();
                }
            }
        }

        public ImageButton(string baseName, bool active)
        {
            BaseName = baseName;
            isActive = active;
        }

        public ImageButton(string imageFolder, string baseName, Point location)
        {
            this.imageFolder = imageFolder;
            BaseName = baseName;
            isActive = true;
            Location = location;
        }

        private void SetToActivePicture()
        {
            Image = Image.FromStream(new FileStream(imageFolder + BaseName + ".png", FileMode.Open, FileAccess.Read));
        }

        private void SetToInactivePicture()
        {
            if (System.IO.File.Exists(imageFolder + BaseName + "_deactivated.png"))
                Image = Image.FromStream(new FileStream(imageFolder + BaseName + "_deactivated.png", FileMode.Open, FileAccess.Read));
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (is_active)
                Image = Image.FromStream(new FileStream(imageFolder + BaseName + ".png", FileMode.Open, FileAccess.Read));
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (is_active)
                Image = Image.FromStream(new FileStream(imageFolder + BaseName + "_down.png", FileMode.Open, FileAccess.Read));
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            if (is_active)
                Image = Image.FromStream(new FileStream(imageFolder + BaseName + "_hover.png", FileMode.Open, FileAccess.Read));
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            if (is_active)
                Image = Image.FromStream(new FileStream(imageFolder + BaseName + ".png", FileMode.Open, FileAccess.Read));
        }
    }
}
